#include <iostream>
#include "Tree.h"

using namespace std;

int main()
{
    TreapTree Test;
    printf("操作代码如下：\n"
           "1.插入\n"
           "2.删除\n"
           "3.查询排名\n"
           "4.查询权重\n"
           "5.查询前驱\n"
           "6.查询后继\n");
    cout << "输入Q退出" << endl;
    int Check;
    while (scanf("%d", &Check))   //循环读取指令
    {
        switch (Check)
        {
            case 1:
            {
                int Value;
                cout << "请输入权重" << endl;
                cin >> Value;
                auto Result = Test.Insert(Value);
                if (Result == ECOperateResult::Success) { cout << "插入成功" << endl; }
                else { cout << "插入失败，错误代码" << Result << endl; }
            }
                break;
            case 2:
            {
                int Value;
                cout << "请输入权重" << endl;
                cin >> Value;
                auto Result = Test.Delete(Value);
                if (Result == ECOperateResult::Success) { cout << "删除成功" << endl; }
                else { cout << "删除失败，错误代码" << Result << endl; }
            }
                break;
            case 3:
            {
                int Value;
                cout << "请输入权重" << endl;
                cin >> Value;
                auto Result = Test.FindRank(Value);
                if (Result == std::numeric_limits<uint32_t>::max()) { cout << "查询失败，错误代码" << Result << endl; }
                else { cout << "查询成功，排名为：" << Result << endl; }
            }
                break;
            case 4:
            {
                int Value;
                cout << "请输入排名" << endl;
                cin >> Value;
                auto Result = Test.FindValue(Value);
                if (Result == std::numeric_limits<uint32_t>::max()) { cout << "查询失败，错误代码" << Result << endl; }
                else { cout << "查询成功" << Result << endl; }
            }
                break;
            case 5:
            {
                int Value;
                cout << "请输入权重" << endl;
                cin >> Value;
                auto Result = Test.FindPredecessor(Value);
                if (Result != std::numeric_limits<uint32_t>::max()) { cout << "查询成功，结果为:" << Result << endl; }
                else { cout << "查询失败，错误代码" << Result << endl; }
            }
                break;
            case 6:
            {
                int Value;
                cout << "请输入权重" << endl;
                cin >> Value;
                auto Result = Test.FindSuccessor(Value);
                if (Result != std::numeric_limits<uint32_t>::max()) { cout << "查询成功，结果为:" << Result << endl; }
                else { cout << "查询失败，错误代码" << Result << endl; }
            }
                break;
            default:
                return 0;
        }
    }
}